<?php

class Model_Author
{

    public static function getAll ()
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query('SELECT * FROM Author WHERE Status = ?', 
                    array(
                            1
                    ));
            
            return $stmt->fetchAll();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function get ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query(
                    'SELECT * FROM Author WHERE AuthorID = ? AND Status = ?', 
                    array(
                            $params['author_id'],
                            1
                    ));
            
            return $stmt->fetch();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function find ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query('SELECT * FROM Author WHERE Name LIKE "%' . $params['q'] . '%" AND Status = ?', 
                    array(                        
                        1
                    ));
            
            return $stmt->fetchAll();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function create ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query('INSERT INTO Author VALUES (?,?,?,?)', 
                    array(
                            null,
                            $params['Name'],
                            microtime(true),
                            1
                    ));
            
            return $stmt->rowCount();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function edit ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query(
                    'UPDATE Author SET Name = ? WHERE AuthorID = ?', 
                    array(
                            $params['Name'],
                            $params['AuthorID']
                    ));
            
            return $stmt->rowCount();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function delete ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query('DELETE FROM Author WHERE AuthorID = ?', 
                    array(
                            $params['AuthorID']
                    ));
            
            return $stmt->rowCount();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }
}
